package edu.uwm.universitydb.dao.dept;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.uwm.universitydb.framework.nontransactional.db.command.impl.NonTransactionalDBFetchCommandBase;
import edu.uwm.universitydb.model.Department;

public class SearchDepartmentByID extends NonTransactionalDBFetchCommandBase<Department>
{

    /** string representing user name column in result set */
    private static final String TUPLE_COLUMN_NAME = "deptname";

    /** string representing role id column in result set */
    private static final String TUPLE_COLUMN_DEPTID = "dno";

    /** string representing the SQL query */
    private static final String SQL_QUERY =
        "select university.department.name "
            + TUPLE_COLUMN_NAME
            + ", university.department.dno "
            + TUPLE_COLUMN_DEPTID
            + " from university.department "
            + "where university.department.dno = ?";

    private final int mDeptId;

    /**
     * 
     * @param userName
     */
    public SearchDepartmentByID(final int did)
    {
    	mDeptId = did;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.cs757.framework.db.command.DataFetchCommand#processResult(java.sql.ResultSet)
     */
    @Override
    public Department processResult(final ResultSet result) throws SQLException
    {
        // create a list
        while (result.next())
        {
            // process one tuple
            final String name = result.getString(TUPLE_COLUMN_NAME);
            final Integer dept = result.getInt(TUPLE_COLUMN_DEPTID);
            // add it to list
            return new Department(dept, name);

        }
        // return the list
        //TODO throw exception
        return null;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.cs757.framework.db.command.DBCommand#getSQL()
     */
    @Override
    public String getSQL()
    {
        return SQL_QUERY;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.cs757.framework.db.command.DBCommand#setParameters(java.sql.PreparedStatement)
     */
    @Override
    public void setParameters(final PreparedStatement pStmt) throws SQLException
    {
        pStmt.setInt(1, mDeptId);
    }

}